<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style type="text/css">
        .container {
            background-color: #fff;
            border: 1px solid #e4e4e4;
            border-radius: 5px;
            height: 500px;
            width: 500px;
            border: 3px dotted #eee;
            margin: 0 auto;
            margin-top: 40px;
            padding: 40px;
        }
        ul {
            margin-top: 20px;
        }
        .folder {
            height: 26px;
            width: 26px;
            background: url(./icon.png) -594px -862px no-repeat;
            cursor: pointer;
        }
        .file {
            height: 26px;
            width: 26px;
            background: url(./icon.png)  -594px -68px no-repeat;
            cursor: pointer;
        }
        li {
            list-style: none;
            padding: 0;
            margin: 0;
            display: flex;
            display: -webkit-flex;
            margin-top: 10px;
            border-bottom: 1px solid #f2f6fd;
            padding-bottom: 10px;
        }
        li a, li span {
            text-decoration: none;
            font-size: 12px;
            line-height: 26px;
            color: black;
            margin-left: 20px;
            font-family: "Microsoft YaHei";
        }
        li span {
            /* margin-left: 50px; */
            width: 150px;
            text-align: center;
        }
        li a {
            width: 50px;
            text-align: center;
        }
        li .del {
            margin-right: 0px!important;
        }
        .title {
            display: flex;
            /* display: -webkit-flex; */
            font-size: 12px;
            /* justify-content: space-around; */
            margin-left: 60px;
        }
        
    </style>
</head>
<body>
    <div class="container">
        <div class="title">
            <span>类型</span>
            <span style="margin-left: 40px;">文件名</span>
            <span style="margin-left: 80px;">大小</span>
            <span style="margin-left: 100px;">时间</span>
            <span style="margin-left: 73px;">操作</span>
        </div>
        <ul>
            
        </ul> 
    </div>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
    $.get("/getlist", function(data) {
        console.log(data);
        var list = JSON.parse(data) //得到数据
        for(var i = 0; i < list.length; i++) { //遍历得到动态数据
            var liNode = "<li><div class='"+ list[i].fileType +"'></div>" +
                            "<a href='' class='filename' id='file"+ list[i].fileTime +"'>" + list[i].fileName + "</a>" +
                            "<span>" + list[i].fileSize + "</span>" +
                            "<span class='time'>" + list[i].fileTime + "</span>" +
                            "<a href='###' class='del' id='del"+ list[i].fileTime +"' >删除</a></li>";
            $("ul").append(liNode);
        }
    })
    $("ul").on("click",".del",function(e){
        e.preventDefault();
        var delLink = this;
        $.get("/del?delId=" + $(this).attr("id"), function(data) {
            console.log(data);
            if(data.length > 0) {
                $(delLink).parent().remove();
            }
        })
    })
    $("ul").on("click", ".filename", function(e) {
        e.preventDefault();
        var fileNameLink = this;
        $.get("/getfile?fileId=" + $(this).attr("id"), function(data) {
            if($(fileNameLink).prev().attr("class") == "file") {
                document.write(data);
            } 
            else {
                var list = JSON.parse(data);
                $("ul").html("");
                for(var i = 0; i < list.length; i++) {
                    var liNode = "<li><div class='"+ list[i].fileType +"'></div>" +
                                    "<a href='' class='filename' id='file"+ list[i].fileTime +"'>" + list[i].fileName + "</a>" +
                                    "<span>" + list[i].fileSize + "</span>" +
                                    "<span class='time'>" + list[i].fileTime + "</span>" +
                                    "<a href='###' class='del' id='del"+ list[i].fileTime +"' >删除</a></li>";
                    $("ul").append(liNode);
                }
            }
        })
    })
</script>
</html>